/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao.erp;

import cn.ekuma.data.dao.ViewDAO;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SentenceList;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.bean.erp.viewbean.FindOrdersInfo;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class FindOrdersInfoDAO extends ViewDAO<FindOrdersInfo>{

	public static String LIST_SQL="SELECT T.ORDERID, E.NAME, T.ORDERTYPE, T.CURDATE, S.NAME, P.NAME, C.NAME ,T.TOTALPRICE,T.ID,T.MEMO,T.STATE,PE.NAME,T.PRICERATE,SE.NAME,T.DEFINETYPEREF,T.DEBT,T.REFDATE "
        + "FROM ORDERS T LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN LOCATIONS S ON S.ID = T.LOCATIONREF LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID  LEFT OUTER JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID LEFT OUTER JOIN PEOPLE PE ON T.REFERENCEPERSON = PE.ID LEFT OUTER JOIN LOCATIONS SE ON T.LOCATIONREF1=SE.ID ";
   
	public static String LIST_Anlysis_SQL="SELECT T.ORDERID, E.NAME, T.ORDERTYPE, T.CURDATE, S.NAME, P.NAME, C.NAME ,T.TOTALPRICE,T.ID,T.MEMO,T.STATE,PE.NAME,T.PRICERATE,SE.NAME,T.DEFINETYPEREF,T.DEBT,T.REFDATE "
        + "FROM ORDERS T LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN LOCATIONS S ON S.ID = T.LOCATIONREF LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID  LEFT OUTER JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID LEFT OUTER JOIN PEOPLE PE ON T.REFERENCEPERSON = PE.ID LEFT OUTER JOIN LOCATIONS SE ON T.LOCATIONREF1=SE.ID LEFT OUTER JOIN OPERATION_CUSTOMER OC ON C.ID=OC.CUSTOMER ";

	
	public static String LIST_SHARD_SQL="SELECT T.ORDERID, E.NAME, T.ORDERTYPE, T.CURDATE, S.NAME, P.NAME, C.NAME ,T.TOTALPRICE,T.ID,T.MEMO,T.STATE,PE.NAME,T.PRICERATE,SE.NAME,T.DEFINETYPEREF,T.DEBT,T.REFDATE "
        + "FROM SHAREDORDERS T LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN LOCATIONS S ON S.ID = T.LOCATIONREF LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID  LEFT OUTER JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID LEFT OUTER JOIN PEOPLE PE ON T.REFERENCEPERSON = PE.ID LEFT OUTER JOIN LOCATIONS SE ON T.LOCATIONREF1=SE.ID ";

	public static String LIST_LINK_SHARD_SQL="SELECT T.ORDERID, E.NAME, T.ORDERTYPE, T.CURDATE, S.NAME, P.NAME, C.NAME ,T.TOTALPRICE,T.ID,T.MEMO,T.STATE,PE.NAME,T.PRICERATE,SE.NAME,T.DEFINETYPEREF,T.DEBT,T.REFDATE "
        + "FROM SHARED_LINK_ORDERS T LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN LOCATIONS S ON S.ID = T.LOCATIONREF LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID  LEFT OUTER JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID LEFT OUTER JOIN PEOPLE PE ON T.REFERENCEPERSON = PE.ID LEFT OUTER JOIN LOCATIONS SE ON T.LOCATIONREF1=SE.ID ";

        
    public FindOrdersInfoDAO(I_Session s) {
        super(s);
    }

    public FindOrdersInfo readValues(DataRead dr,FindOrdersInfo obj) throws BasicException {
        if(obj==null)
        	obj=new FindOrdersInfo();
        obj.orderid = dr.getString(1);
        obj.defineType= dr.getString(2);
        obj.ordertype = dr.getInt(3);
        obj.date = dr.getTimestamp(4);
        obj.location=dr.getString(5);
        obj.name = dr.getString(6);
        obj.customer = dr.getString(7);
        obj.totalPrice=(dr.getObject(8) == null) ? 0.0 : dr.getDouble(8).doubleValue();
        obj.id=dr.getString(9);
        obj.memo=dr.getString(10);
        obj.currentState=dr.getString(11);
        obj.m_sResponse=dr.getString(12);
        obj.m_dPriceRate=dr.getDouble(13);
        obj.locationRef1=dr.getString(14);
        obj.setDefineTypeId(dr.getString(15));
        obj.setDebt(dr.getDouble(16));
        obj.setM_dRefDate(dr.getTimestamp(17));
        return obj;
    }

    @Override
    public List<FindOrdersInfo> list(Object filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
        		LIST_SQL+ "WHERE ?(QBF_FILTER)  ORDER BY T.CURDATE DESC ", new String[]{"T.ORDERID", "T.DEFINETYPEREF", "T.ORDERTYPE", "T.CURDATE", "T.CURDATE", "P.NAME", "C.NAME", "T.LOCATIONREF", "T.TOTALPRICE"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.INT, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.DOUBLE}), this).list(filter);
    }

    public SentenceList getSharedOrderListQBF() {
        return new StaticSentence(s
            , new QBFBuilder(
            		LIST_SHARD_SQL + "WHERE ?(QBF_FILTER) AND T.LOCKED="+s.getDB().FALSE()+" ORDER BY T.CURDATE DESC ", new String[] {"T.ORDERID", "T.DEFINETYPEREF", "T.ORDERTYPE","T.CURDATE", "T.CURDATE", "P.NAME", "C.NAME","T.LOCATIONREF","T.TOTALPRICE"})
            , new SerializerWriteBasic(new Datas[] {Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.INT, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.DOUBLE})
            , this);
    }
    
    public List<FindOrdersInfo> listShardOrder(Object filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
        		LIST_SHARD_SQL + "WHERE ?(QBF_FILTER) ORDER BY T.CURDATE DESC ", new String[] {"T.ORDERID", "T.DEFINETYPEREF", "T.ORDERTYPE","T.CURDATE", "T.CURDATE", "P.NAME", "C.NAME","T.LOCATIONREF","T.TOTALPRICE"})
        , new SerializerWriteBasic(new Datas[] {Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.INT, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.DOUBLE})
        , this).list(filter);
    }
    
    public SentenceList getSharedLinkOrderListQBF() {
        return new StaticSentence(s
            , new QBFBuilder(
            		LIST_LINK_SHARD_SQL + "WHERE ?(QBF_FILTER) AND T.LOCKED="+s.getDB().FALSE()+" ORDER BY T.CURDATE DESC ", new String[] {"T.ORDERID", "T.DEFINETYPEREF", "T.ORDERTYPE","T.CURDATE", "T.CURDATE", "P.NAME", "C.NAME","T.LOCATIONREF","T.TOTALPRICE"})
            , new SerializerWriteBasic(new Datas[] {Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.INT, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.DOUBLE})
            , this);
    }
    
    
    public List<FindOrdersInfo> listOrderInfoByAnlysis(Object filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
        		LIST_Anlysis_SQL+ "WHERE ?(QBF_FILTER)  ORDER BY T.CURDATE DESC ", new String[]{"T.ORDERID", "T.DEFINETYPEREF", "T.ORDERTYPE", "T.CURDATE", "T.CURDATE", "P.NAME", "C.NAME", "T.LOCATIONREF", "T.TOTALPRICE","OC.OPERATIONCATEGORIE","T.DEBT"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.INT, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.DOUBLE, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.DOUBLE}), this).list(filter);
    }

    public FindOrdersInfo find(String id) throws BasicException {
        return  (FindOrdersInfo) new PreparedSentence(s,LIST_SQL+ " WHERE T.ID=?  ", SerializerWriteString.INSTANCE, this).find(id);
    }
    
    @Override
    public Class getSuportClass() {
        return FindOrdersInfo.class;
    }
}
